<?php
if( !defined('IN_INDEX') )
{
	printError("ACCESS DENIED", "This page is hidden.");
	exit;
}

$task = $_REQUEST['task'];

switch ($task){
	default:
	case 'auto_comp_show_all_gym':
		auto_comp_show_all_gym();break;
	case 'list_pendding_gyms':
		list_pendding_gyms();break;	
}

function list_pendding_gyms(){
	$gym_array = get_all_gyms_unactived();
	
	$UI = "";
	foreach($gym_array as $array){
		$UI .= "<a href='index.php?action=gym&sub=view&gym_id=".$array['gym_id']."' >".htmlentities($array['gym_name'])."</a>"."<br />";
	}
	echo $UI;
	exit();
}


function auto_comp_show_all_gym(){
	$qr =  $_GET['query'];
	$qr = mysql_real_escape_string($qr);
	$gym_array = get_array_gym($qr); 
 	
	$suggest = $data = array();
	
	foreach($gym_array as $each){
		if($each['gym_activated'] == 1){
			$suggest[] = ($each['gym_name']);//htmlentities
			$data[] = $each['gym_id'];
		}
	}
	$rsJS = array('query' =>$qr , 'suggestions' => $suggest , 'data' =>$data );
	
	$return = json_encode($rsJS);
	echo $return;
	exit();
}

function get_all_gyms_unactived(){
	global $db;
	
	$sql = "SELECT B.* FROM ".TRAINING_TABLE." AS A INNER JOIN ".GYM_TABLE." AS B ON A.gym_id=B.gym_id WHERE B.gym_activated !='1' GROUP BY(A.gym_id)";
 
	if(!$result = $db->sql_query($sql))
	{
		printError("SQL ERROR", mysql_error($sql));
	}
	
	$gym_array = array();
	while($tmp = $db->sql_fetchrow($result)){
		$gym_array[]= $tmp;
	}
	return $gym_array;
}

function get_array_gym($qr){
	global $db;
	
	$qrc = mb_strtolower($qr, 'UTF-8');  
	$sql = "SELECT * FROM tbl_gym WHERE LOWER(gym_name) LIKE '$qrc%'";  
	$db->sql_query("SET NAMES 'utf8'");
	if(!$result = $db->sql_query($sql))
	{
		printerror("SQL ERROR", mysql_error($sql) ."<br />" . mysql_errno($sql));
	}
	$array = array();
	while($tmp = $db->sql_fetchrow($result)){
		$array[]= $tmp;
	}
	 
	return $array;
}
